//chrome.extension.sendRequest({greeting: "hello"}, function(response) {
//  console.log(response.farewell);
//});

//var debug = true;
var debug = false;

// First run code
var intervalID = window.setInterval(run, 2000);
var lastInboxCount;
var lastChatMessagesCount;
var chatNotification;
var desktopNotification;
var checkArea;

function run() {
  chrome.extension.sendRequest({type: "getvars"}, function(response) {
    chatNotification = response.chatNotification == "true" || !response.chatNotification;
	desktopNotification = response.desktopNotification == "true" || !response.desktopNotification;
	checkArea = response.checkArea ? response.checkArea : "0";
  });
  var inboxCount;
  switch (checkArea) {
    case "0":
      inboxCount = getInboxCount(0);
	  break;
	case "1":
	  inboxCount = getInboxCount(1);
	  break;
  }
  var chatMessages = getChatMessages();
  if(inboxCount != lastInboxCount || (chatNotification && chatMessages != lastChatMessagesCount) ) {
    if(inboxCount != lastInboxCount && inboxCount > 0 && desktopNotification) {
      chrome.extension.sendRequest({type: 'email', inboxCount: inboxCount});
    }
	if(chatNotification) {
	  if(chatMessages != lastChatMessagesCount && chatMessages > 0 && desktopNotification) {
	    chrome.extension.sendRequest({type: 'chat'});
	  }
	  changeFavicon(inboxCount, chatMessages);
	} else {
	  changeFavicon(inboxCount, 0);
	}
	lastInboxCount = inboxCount;
	lastChatMessagesCount = chatMessages;
  }
}


// inbox: 0 -> Priority Inbox
//        1 -> Inbox
// retun: NaN if is not able to parse the string representing the number of unread mails.
function getInboxCount(inbox) {
  var theInboxText = '';
  if (debug) {
    theInboxText = "Priority Inbox (2)";
  }
  else {
    var frame = top.document.getElementById('canvas_frame');
    if (frame) {
      var theInboxSpan = frame.contentDocument.getElementsByClassName('n0')[inbox];
      if(theInboxSpan) theInboxText = theInboxSpan.textContent;
    }
  }
  var inboxCountRegExp = new RegExp(".+[(](.+)[)]");
  var inboxCount = inboxCountRegExp.test(theInboxText)? parseInt(inboxCountRegExp.exec(theInboxText)[1], 10) : 0;
  return inboxCount;
}


function getChatMessages() {
  var chatMessagesCount = 0;
  if (debug) {
    chatMessagesCount = 1;
  }
  else {
    var frame = top.document.getElementById('canvas_frame');
    if (frame) {
      chatMessagesCount = frame.contentDocument.getElementsByClassName('vE').length;
	}
  }
  return chatMessagesCount;
}

// Change the browser favicon
// Partially taken from: http://www.bardavid.com/test/changefavicon.html
function changeFavicon(inboxCount, chatMessages) {
  var head = document.getElementsByTagName("head")[0];
  var links = head.getElementsByTagName("link");
  // Remove previous favicon
  for (var i = 0; i < links.length; i++) {
	if (links[i].rel=="shortcut icon") {
	  head.removeChild(links[i]);
	  break;
	}
  }
  // Add new favicon
  var faviconUrls = new Array(
    '',
	'',	
	'',
	'',
	''
  );
  var faviconChatUrls = new Array(
    '',
	'',
	'',
	'',
	''
  );
  //var url = "http://www.bardavid.com/test/icon0.png";
  var link = document.createElement("link");
  link.type = "image/png";
  link.rel = "shortcut icon";
  if(chatMessages > 0) {
    if (inboxCount <= 3) {
      link.href = faviconChatUrls[inboxCount];
	} else {
	  link.href = faviconChatUrls[4];
	}
  } else {
    if (inboxCount <= 3) {
      link.href = faviconUrls[inboxCount];
	}
	else {
	  link.href = faviconUrls[4];
	}
  }
  head.appendChild(link);
}
